<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- 头部布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="tree" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件 -->
        <!-- data: 树形组件数据 -->
        <!-- props:指定树形组件的文件或后代的键名 -->
        <!-- default-expand-all 默认展开节点 -->
        <el-tree :data="list" default-expand-all>
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="tree" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="showDialog=true">添加子部门</el-dropdown-item>
                        <el-dropdown-item>编辑部门</el-dropdown-item>
                        <el-dropdown-item>删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 实现新增部门或编辑部门 -->
      <el-dialog
        title="添加或编辑"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <addorEdit />
      </el-dialog>
      <!-- 实现新增部门或编辑部门 -->
    </div>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListTotreeList } from '@/utils'
import addorEdit from './deptDialog.vue'
export default {
  components: { addorEdit },
  data() {
    return {
      // 保存部门数据
      list: [],
      showDialog: false
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res, 'getDepartments')
        // 保存数据
        const company = res.data.depts.shift()
        console.log(company, 'loadDepartments')
        // tranListTotreeList(res.data.depts) 将平铺数据转换为树状图数据
        this.list = tranListTotreeList(res.data.depts)
        // shift会取出数组第一个元素，同时会改变源数组
        // const company = res.data.depts.shift()
        // console.log('loadDepartments', company)
        // this.list = res.data.depts
        // 后端返回的数据中 第一条是公司信息，它应该直接就是tree的根节点
        // 为了后面计算方便，把它直接取出来
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style></style>
